package com.coming.mySpring.org.controller;

import com.coming.mySpring.org.domain.Department;
import com.coming.mySpring.org.query.DepartmentQuery;
import com.coming.mySpring.org.service.IDepartmentService;
import com.coming.mySpring.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description = "部门相关的crud功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询
     * @ResponseBody   转换为json格式
     * @return
     */
    @ApiOperation(value ="查询全部")
//    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @PostMapping("/list")
    public List<Department> loadAll(){

        return departmentService.loadAll();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public AjaxResult loadOne(@PathVariable("id") Long id){
        try {
            Department department = departmentService.loadOne(id);
            return AjaxResult.success().setDate(department);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping("/queryList")
    @ApiOperation(value = "分页查询")
    public AjaxResult queryList(@RequestBody DepartmentQuery query){
        try {
            List<Department> pageQuery = departmentService.queryList(query);
            Long queryCount = departmentService.queryCount(query);
            return AjaxResult.success().setDate(pageQuery).setTotal(queryCount);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult Delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }

    /**
     * 保存   /   修改
     * @param department
     */
    @PutMapping
    @ApiOperation(value = "保存与修改")
    public AjaxResult save(@RequestBody Department department){
        /*
            一、这里的保存分为两种
                1、为添加保存
                2、为修改保存
         */
        try {
            if(department.getId() !=null){
                //当id不为空的时候,为修改保存
                departmentService.update(department);
            }else{
                //当id为空的时候,为修改保存
                departmentService.save(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    @PatchMapping("/del")
    @ApiOperation(value = "批量删除")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }

}
